package cn.liangcaomei.eduservice.service.impl;

import cn.liangcaomei.eduservice.entity.EduChapter;
import cn.liangcaomei.eduservice.entity.EduVideo;
import cn.liangcaomei.eduservice.entity.chapter.ChapterVo;
import cn.liangcaomei.eduservice.entity.chapter.VideVo;
import cn.liangcaomei.eduservice.mapper.EduChapterMapper;
import cn.liangcaomei.eduservice.service.EduChapterService;
import cn.liangcaomei.eduservice.service.EduVideoService;
import cn.liangcaomei.servicebase.exceptionhandler.CustException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author LiuJie
 * @since 2024-01-27
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    //注入videoService
    @Autowired
    private EduVideoService eduVideoService;

    //課程大綱列表,根據課程Id查詢小節
    @Override
    public List<ChapterVo> getChapterVideoByCoutseId(String courseId) {
        //1 根據課程id查詢所有的章節集合
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> chaptersList = baseMapper.selectList(wrapperChapter);
        
        //2 根據課程id查詢所有章節的小節集合
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> videoList = eduVideoService.list(wrapperVideo);

        //創建list集合，封裝最總的數據
        List<ChapterVo> finalList = new ArrayList<>();

        //3 遍歷章節集合，進行封裝
        for (int i = 0; i < chaptersList.size(); i++) {
            EduChapter eduChapter = chaptersList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            //把 eduChapter 對象值複製到 chapterVo 中
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把 chapterVo 放到 finalList 中
            finalList.add(chapterVo);
            
            //創建集合，用於封裝對於的章節的小節
            List<VideVo> VList = new ArrayList<>();
            //4 遍歷章節下的小節list集合，進行封裝
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    //進行封裝
                    VideVo videVo = new VideVo();
                    BeanUtils.copyProperties(eduVideo,videVo);
                    VList.add(videVo);
                }
            }
            //把封裝之后的小節集合封裝到對於的章節中去
            chapterVo.setChildren(VList);
        }
        return finalList;
    }

    //删除方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //1 根据 chapterId 查询是否有小节，如果有数据，则不让删除数据
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0) {
            //表示有数据，不让删除
            throw new CustException(20001,"本章节下有小节，不能删除！");
        } else {
            //表示没有数据，可以删除
            int result = baseMapper.deleteById(chapterId);
            //如果 result > 0 则返回 true，否则返回 false
            return result>0;
        }

    }

    //根据课程id 删除章节
    @Override
    public void removeChapterCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }


}
